<p:config xmlns:p="http://www.orbeon.com/oxf/pipeline" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:oxf="http://www.orbeon.com/oxf/processors" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xxforms="http://orbeon.org/oxf/xml/xforms" xmlns:xdb="http://orbeon.org/oxf/xml/xmldb" xmlns:xu="http://www.xmldb.org/xupdate">

	<p:param type="output" name="data"/>

	<p:processor name="oxf:request">
		<p:input name="config">
			<config >
				<include>/request/parameters</include>
				<include>/request/request-uri</include>
			</config>
		</p:input>
		<p:output name="data" id="request"/>
	</p:processor>
	<p:choose href="#request">
		<!--No file uploaded-->
		<p:when test="/request/parameters/parameter[name = 'widgetFile']/value = ''">
			<p:processor name="oxf:xslt">
				<p:input name="data" href="#request#xpointer(/request/parameters)"/>
				<p:output name="data" id="query"/>
				<p:input name="config">
					<xdb:query xsl:version="2.0" collection="/db/portal">
xquery version "1.0";
let $wg := /widgetsList/category/widget[@id='<xsl:value-of select="/parameters/parameter[name = 'widget']/value"/>'] return <message>{
update insert $wg into /widgetsList/category[@id='<xsl:value-of select="/parameters/parameter[name = 'category']/value"/>']
}Widget '{$wg/name/text()}' has been successfully updated.{update delete $wg}</message>
					</xdb:query>
				</p:input>
			</p:processor>
			<p:processor name="oxf:xmldb-query">
				<p:input name="datasource" href="/config/datasource.xml"/>
				<p:input name="query" href="#query"/>
				<p:output name="data" id="message"/>
			</p:processor>
		</p:when>
		<!--New manifest-->
		<p:when test="contains(/request/parameters/parameter[name = 'widgetFile']/content-type, 'xml')">

			<p:processor name="oxf:to-xml-converter">
				<p:input name="data" href="#request#xpointer(/request/parameters/parameter[name = 'widgetFile']/value)"/>
				<p:input name="config">
					<config/>
				</p:input>
				<p:output name="data" id="validate"/>
			</p:processor>

			<!--manifest validation-->
			<p:processor name="oxf:validation">
				<p:input name="config">
					<config>
						<decorate>true</decorate>
					</config>
				</p:input>
				<p:input name="schema" href="/schema/manifest.xsd"/>
				<p:input name="data" href="#validate"/>
				<p:output name="data" id="manifest"/>
			</p:processor>
			<p:choose href="#manifest">
				<p:when test="exists(//*:error)">
					<p:processor name="oxf:xslt">
						<p:input name="data" href="#manifest"/>
						<p:output name="data" id="error_doc"/>
						<p:input name="config" href="/config/manifest_debug.xsl"/>
					</p:processor>
					<p:processor name="oxf:xml-converter">
						<p:input name="config">
							<config>
								<content-type>text/html</content-type>
								<encoding>utf-8</encoding>
								<version>1.0</version>
								<omit-xml-declaration>true</omit-xml-declaration>
							</config>
						</p:input>
						<p:input name="data" href="#error_doc"/>
						<p:output name="data" id="html-doc"/>
					</p:processor>

					<p:processor name="oxf:http-serializer">
						<p:input name="config">
							<config>
								<content-type>text/html</content-type>
								<force-content-type>true</force-content-type>
							</config>
						</p:input>
						<p:input name="data" href="#html-doc"/>
					</p:processor>
					<p:processor name="oxf:identity">

						<p:output name="data" id="message"/>
						<p:input name="data">
							<error>An error occured during widget installation.</error>
						</p:input>
					</p:processor>

				</p:when>
				<p:otherwise>
					<p:processor name="oxf:xslt">
						<p:input name="data" href="#request"/>
						<p:output name="data" id="config"/>
						<p:input name="config">
							<config xsl:version="2.0">
								<file>webapps<xsl:value-of select="substring-before(/request/request-uri, '/admin/action/')"/>/WEB-INF/resources/widgets/<xsl:value-of select="/request/parameters/parameter[name = 'widget']/value"/>/config.xml</file>
								<make-directories>true</make-directories>
								<append>false</append>
								<xsl:copy-of select="/request/parameters/parameter[name='widgetFile']/content-type"/>
							</config>
						</p:input>
					</p:processor>
					<p:processor name="oxf:file-serializer">
						<p:input name="config" href="#config"/>
						<p:input name="data" href="#manifest"/>
					</p:processor>
					<p:processor name="oxf:xslt">
						<p:input name="data" href="#request#xpointer(/request/parameters)"/>
						<p:output name="data" id="query"/>
						<p:input name="config">
							<xdb:query xsl:version="2.0" collection="/db/portal">
xquery version "1.0";
let $wg := /widgetsList/category/widget[@id='<xsl:value-of select="/parameters/parameter[name = 'widget']/value"/>'] return <message>{
update insert $wg into /widgetsList/category[@id='<xsl:value-of select="/parameters/parameter[name = 'category']/value"/>']
}Widget '{$wg/name/text()}' has been successfully updated.{update delete $wg}</message>
							</xdb:query>
						</p:input>
					</p:processor>
					<p:processor name="oxf:xmldb-query">
						<p:input name="datasource" href="/config/datasource.xml"/>
						<p:input name="query" href="#query"/>
						<p:output name="data" id="message"/>
					</p:processor>
				</p:otherwise>
			</p:choose>

		</p:when>
		<!--Zip installation-->
		<p:when test="contains(/request/parameters/parameter[name = 'widgetFile']/content-type, 'zip')">
			<p:processor name="oxf:xslt">
				<p:input name="data" href="#request"/>
				<p:output name="data" id="file-addr"/>
				<p:input name="config">
					<config xsl:version="2.0">
						<url>
							<xsl:value-of select="/request/parameters/parameter[name = 'widgetFile']/value/text()"/>
						</url>
						<xsl:copy-of select="/request/parameters/parameter[name='widgetFile']/content-type"/>
					</config>
				</p:input>
			</p:processor>
			<p:processor name="oxf:url-generator">
				<p:input name="config" href="#file-addr"/>
				<p:output name="data" id="file"/>
			</p:processor>

			<p:processor name="oxf:unzip">
				<p:input name="data" href="#file"/>
				<p:output name="data" id="zip-file-list"/>
			</p:processor>

			<!--Read config file-->
			<p:processor name="oxf:xslt">
				<p:input name="data" href="#zip-file-list"/>
				<p:output name="data" id="config_url"/>
				<p:input name="config">
					<config xsl:version="2.0">
						<url>
							<xsl:value-of select="/files/file[@name='config.xml']"/>
						</url>
						<content-type>text/xml</content-type>
					</config>
				</p:input>
			</p:processor>
			<p:processor name="oxf:url-generator">
				<p:input name="config" href="#config_url"/>
				<p:output name="data" id="validate"/>
			</p:processor>

			<!--manifest validation-->
			<p:processor name="oxf:validation">
				<p:input name="config">
					<config>
						<decorate>true</decorate>
					</config>
				</p:input>
				<p:input name="schema" href="/schema/manifest.xsd"/>
				<p:input name="data" href="#validate"/>
				<p:output name="data" id="manifest"/>
			</p:processor>

			<p:choose href="#manifest">
				<!--Manifest error-->
				<p:when test="exists(//*:error)">
					<p:processor name="oxf:xslt">
						<p:input name="data" href="#manifest"/>
						<p:output name="data" id="error_doc"/>
						<p:input name="config" href="/config/manifest_debug.xsl"/>
					</p:processor>
					<p:processor name="oxf:xml-converter">
						<p:input name="config">
							<config>
								<content-type>text/html</content-type>
								<encoding>utf-8</encoding>
								<version>1.0</version>
								<omit-xml-declaration>true</omit-xml-declaration>
							</config>
						</p:input>
						<p:input name="data" href="#error_doc"/>
						<p:output name="data" id="html-doc"/>
					</p:processor>

					<p:processor name="oxf:http-serializer">
						<p:input name="config">
							<config>
								<content-type>text/html</content-type>
								<force-content-type>true</force-content-type>
							</config>
						</p:input>
						<p:input name="data" href="#html-doc"/>
					</p:processor>

					<p:processor name="oxf:identity">
						<p:input name="data">
							<error>An error occured during widget installation.</error>
						</p:input>
						<p:output name="data" id="message"/>
					</p:processor>
				</p:when>
				<p:otherwise>
					
					<p:processor name="oxf:xslt">
						<p:input name="data" href="#zip-file-list"/>
						<p:input name="request" href="#request"/>
						<p:output name="data" id="files-list"/>
						<p:input name="config">
							<xsl:stylesheet version="2.0">
								<xsl:template match="/">
									<xsl:copy>
										<xsl:apply-templates select="@*|node()"/>
									</xsl:copy>		
								</xsl:template>
								<xsl:template match="file[ends-with(@name, '/')]"/>
								<xsl:template match="file" priority="-2">
									<file name="{concat('webapps',substring-before(doc('input:request')/request/request-uri, '/admin/action/'),'/WEB-INF/resources/widgets/',string(doc('input:request')/request/parameters/parameter[name='widget']/value), '/', @name)}">
										<xsl:apply-templates select="@*|node()"/>
									</file>
								</xsl:template>
								<xsl:template match="@name"/>
								<xsl:template match="@*|node()" priority="-3">
									<xsl:copy>
										<xsl:apply-templates select="@*|node()"/>
									</xsl:copy>
								</xsl:template>
							</xsl:stylesheet>
						</p:input>
					</p:processor>

					<!--create config to write-->
					<p:for-each select="/files/file" href="#files-list" root="configs" id="configs">

						<p:processor name="oxf:xslt">
							<p:input name="data" href="current()"/>
							<p:output name="data" id="url"/>
							<p:input name="config">
								<xsl:stylesheet version="2.0">
									<xsl:template match="/">
										<config>
											<url>
												<xsl:value-of select="."/>
											</url>
											<content-type>application/octet-stream</content-type>
										</config>
									</xsl:template>
								</xsl:stylesheet>
							</p:input>
						</p:processor>
						<p:processor name="oxf:url-generator">
							<p:input name="config" href="#url"/>
							<p:output name="data" id="tmp_file"/>
						</p:processor>

						<p:processor name="oxf:xslt">
							<p:input name="data" href="current()"/>
							<p:output name="data" id="config"/>
							<p:input name="config">
								<xsl:stylesheet version="2.0">
									<xsl:variable name="path" select="tokenize(file/@name, '/')"/>
									<xsl:template match="/">
										<config>
											<file>.<xsl:for-each select="$path">
													<xsl:if test="position() != last()">/<xsl:value-of select="."/>
													</xsl:if>
												</xsl:for-each>/<xsl:value-of select="$path[last()]"/>
											</file>
											<make-directories>true</make-directories>
											<append>false</append>

											<content-type>application/octet-stream</content-type>
										</config>
									</xsl:template>
								</xsl:stylesheet>
							</p:input>
						</p:processor>

						<p:processor name="oxf:file-serializer">
							<p:input name="config" href="#config"/>
							<p:input name="data" href="#tmp_file"/>
						</p:processor>

						<p:processor name="oxf:identity">
							<p:input name="data" href="#config"/>
							<p:output name="data" ref="configs"/>
						</p:processor>
					</p:for-each>


					<p:processor name="oxf:null-serializer">
						<p:input name="data" href="#configs"/>
					</p:processor>
					<p:processor name="oxf:xslt">
						<p:input name="data" href="#request#xpointer(/request/parameters)"/>
						<p:output name="data" id="query"/>
						<p:input name="config">
							<xdb:query xsl:version="2.0" collection="/db/portal">
xquery version "1.0";
let $wg := /widgetsList/category/widget[@id='<xsl:value-of select="/parameters/parameter[name = 'widget']/value"/>'] return <message>{
update insert $wg into /widgetsList/category[@id='<xsl:value-of select="/parameters/parameter[name = 'category']/value"/>']
}Widget '{$wg/name/text()}' has been successfully updated.{update delete $wg}</message>
							</xdb:query>
						</p:input>
					</p:processor>
					<p:processor name="oxf:xmldb-query">
						<p:input name="datasource" href="/config/datasource.xml"/>
						<p:input name="query" href="#query"/>
						<p:output name="data" id="message"/>
					</p:processor>
				</p:otherwise>
			</p:choose>
		</p:when>
		<p:otherwise>
			<p:processor name="oxf:identity">
				<p:input name="data">
					<error>An error occured during widget installation.</error>
				</p:input>
				<p:output name="data" id="message"/>
			</p:processor>
		</p:otherwise>
	</p:choose>


	<p:processor name="oxf:pipeline">
		<p:input name="config" href="../widget.xpl"/>
		<p:output name="data" id="datas"/>
	</p:processor>

	<p:processor name="oxf:identity">
		<p:input name="data" href="aggregate('data', #message, #datas#xpointer(/data/*))"/>
		<p:output name="data" ref="data"/>
	</p:processor>

	<!--<p:processor name="oxf:xml-converter">
		<p:input name="config">
			<config>
				<content-type>text/xml</content-type>
				<encoding>utf-8</encoding>
				<version>1.0</version>
				<omit-xml-declaration>true</omit-xml-declaration>
			</config>
		</p:input>
		<p:input name="data" href="#request"/>
		<p:output name="data" id="xml-doc"/>
	</p:processor>

	<p:processor name="oxf:http-serializer">
		<p:input name="config">
			<config>
				<content-type>text/xml</content-type>
				<force-content-type>true</force-content-type>
			</config>
		</p:input>
		<p:input name="data" href="#xml-doc"/>
	</p:processor>-->
</p:config>